$(function() {
		layout();

		secondary_menu();
		
		content_menu();
		selectBtn();
		tabBtn();


	})
function _href(url)
{
	var html="<div id='hy_iframe' style='position:absolute;top:5px;width:100%;height:100%;background: rgba(0, 0, 0, 0.4);'>";
	html+="<div style='background:#fff;width:80%;margin:0 auto;border:1px solid #ddd;height:100%;' class='window_main'>";
	html+="<div style='height:30px;background:#ddd;padding-right:10px' class='window_header'><a class='close_btn f_r'>X</a></div>";
	html+="<iframe  frameborder='0' style='width:100%;height:100%'  src='"+url+"' scrolling='yes'></iframe>";
	html+="</div>";
	html+="</div>";
	if(parent.$('#hy_iframe'))
	{
		parent.$('#hy_iframe').remove();
	}
	parent.$('body').append(html);
	parent.$('.window_header .close_btn').bind('click',function(){
	
		parent.$('#hy_iframe').remove();
	});
	
}
	//下拉列表

function selectBtn() {
		$(".select_btn").click(function() {
			$(this).next().show();
			$(this).mouseleave(function() {
				$(this).next().hide();
			})
		})
		$(".select_btn").next().find("li").click(function() {
			var val = $(this).attr("data-select");
			$(this).parent().parent().parent().find(".select_val").val(val);
			var text = $(this).find("a").text();
			$(this).parent().parent().parent().find(".select_btn").val(text);
			$(".selectBtn").next().hide();
			return false;
		})
	}
	//切换按钮

function tabBtn() {
		$('.tabBtn').live('click', function() {
			var pre = $(this).attr('data-name');
			var check = $(this).attr('data-value');

			$("*[id^=" + pre + "]").each(function() {
				$(this).hide();
			});
			$("#" + pre + check).show();

		});
	}
	//单选按钮

function selectBtn() {
		var other;
		$('.selectBtn').click(function() {

			if ($(this).next('.selectBtn').length > 0) {

				other = $(this).next('.selectBtn');
			} else if ($(this).prev('.selectBtn').length > 0) {

				other = $(this).prev('.selectBtn');
			}

			if (typeof(other) != 'undefined') {
				if ($(this).hasClass('focus')) {
					$(this).removeClass("focus");
					other.addClass("focus");
				} else {
					other.removeClass("focus");
					$(this).addClass("focus");
				}
				$('input[name=' + $(this).attr('data-name') + ']').val($(this).attr('data-value'));
			}

		});

	}
/*
 
 * 切换状态
 * @param table table名
 * @param status_num 状态数
 * @param name 字段名
 * @param id id
 * */
function hy_toggle(_this,name,status_num,id,tables)
{
	
	var url='/?g=admin&m='+getQueryString('m')+'&a=toggle';
	$(_this).html("<img src='/Admin/webroot/images/loading.gif'/>");
	$.post(url,{id:id,status_num:status_num,table:tables,name:name},function(result)
	{
		if(result.error==0)
		{
			$(_this).html("<img src='/Admin/webroot/images/yes.gif'/>");
		}
		else
		{
			$(_this).html("<img src='/Admin/webroot/images/no.gif'/>");
		}
	},'json')
}
	//sousuo

function hy_sort(param, val) {
	var url = window.location.href;
	var arrStr = url.substring(url.indexOf("?") + 1).split("&");
	//return arrStr;

	var new_url = '';
	for (var i = 1; i < arrStr.length; i++) {
		var loc = arrStr[i].indexOf(param + "=");
		if (loc != -1) {

		} else {
			new_url += '&' + arrStr[i];
		}

	}
	
	if (param == 'k') {
		val = $('input[name=k]').val();
	}
	if (val != '' && val != 0) {
		window.location.href = '/?' + arrStr[0] + new_url + '&' + param + '=' + val;
	} else {
       window.location.href = '/?' + arrStr[0] + new_url;
	}

}

function secondary_menu() {

		$(".sec_menu").click(function() {
			if (!$(this).hasClass('focus')) {
				$(this).addClass("focus");
				$(this).next(".sys-menu").slideDown();

			} else {
				$(this).removeClass("focus");
				$(this).next(".sys-menu").slideUp();

			}
		})
	}


function msg_detail(id) {
		$.post('index.php?g=admin&m=msg&a=get_detail', {
			id: id
		}, function(result) {

			if (result) {
				if ($('.msg_detail')) {
					$('.msg_detail').remove();
				}
				var div = document.createElement("div");
				div.innerHTML = result.content;
				$(" body").append("<div class=\'msg_detail\'><div class=\'msg_title\'><a class=\'msg_close\' href=\'javascript:void(0)\'>X</a></div><div class=\'msg_main\'><ul><li><span>电话：</span><pp>" + result.phone + "</p></li><li><span>内容：</span><p><p>" + div.innerText + "</p></li></ul><div style=\'clear:both; height:15px;\'></div></div></div>");
				$('.msg_close').click(function() {
					$('.msg_detail').remove();
				});
			} else {
				alert('error');

			}
		}, 'json');
	}
	//设置封面

function set_cover(id, album_id) {
		$.post('index.php?g=admin&m=photo&a=set_cover', {
			id: id,
			album_id: album_id
		}, function(result) {

			if (result.error > 0) {
				alert(result.content);
			} else {
				alert('设置成功');
				window.history.go(0);
			}
		}, 'json');
	}
	//显示信息

function showMsg(msg, url) {
	var html = '<div class="ajax_msg"><div class="ajax_title"><span>提示信息</span><a class="ajax_close" href="javascript:void(0)">X</a></div><div class="ajax_main"></div></div>';
	parent.$('body').append(html);
	parent.$('.ajax_main').html(msg);
	parent.$('.ajax_msg').show();
	parent.$('.mask').show();
	parent.$('.ajax_close').live('click', function() {
		parent.$('.ajax_msg').remove();
		parent.$('.mask').hide();
	});
	if (typeof(url) != 'undefined' && url != '') {

		parent.$('.ajax_close').click(function() {
			window.location.href = url;
		});
		setTimeout(window_go, 3000);
		//window.location.href=url;
	}

	function window_go() {
		parent.$('.ajax_msg').remove();
		parent.$('.mask').hide();
		window.location.href = url;
	}

}
$(window).resize(function() {
	layout();
	ajax_pos();
})

function ajax_pos() {
	$('.ajax_msg').css({
		position: 'fixed',
		left: ($(window).width() - $('.ajax_msg').outerWidth()) / 4 * 3,
		top: ($(window).height() - $('.ajax_msg').outerHeight()) / 4 * 3 + $(document).scrollTop()
	});

}


function getQueryString(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
	var r = window.location.search.substr(1).match(reg);
	if (r != null) return unescape(r[2]);
	return null;
}

function content_menu() {
	var type = 'hide';
	$("#cont-manager").click(function() {
		if (type == 'hide') {
			$(this).addClass("focus");
			$(this).next(".cont-menu").slideDown();
			type = 'down';
		} else {
			$(this).removeClass("focus");
			$(this).next(".cont-menu").slideUp();
			type = 'hide';
		}
	})
}

function confirm_this(title, action_url) {


	if (window.confirm(title)) {
		window.location.href = action_url;
	}
}

function del_this(_this, url) {


		if (window.confirm('确定要删除吗？')) {
			$.post(url, {}, function(result) {
				//alert(result)
				if (result.error > 0) {
					alert(result.content);
				} else {
					$(_this).parent().parent().hide();
				}
			}, 'json');
		}
	}
	//ajax排序

function ordering(_this, m, a) {
	var g = (getQueryString('g'));
	if (typeof(m) == 'undefined') {
		m = (getQueryString('m'));
	}
	if (typeof(a) == 'undefined') {
		a = 'ordering';
	}

	if ($(_this).next('img').length == 0) {
		$(_this).parent().append('<img src="/Admin/webroot/images/loading.gif"/>')
	} else {
		$(_this).next('img').attr('src', '/Admin/webroot/images/loading.gif').show();
	}

	$.post('index.php?g=' + g + '&m=' + m + '&a=' + a, {
		val: _this.value,
		id: _this.getAttribute('order_id')
	}, function(result) {

		if (result.error == 0) {
			$(_this).next('img').attr('src', '/Admin/webroot/images/yes.gif');


		} else {
			$(_this).next('img').attr('src', '/Admin/webroot/images/no.gif');
		}
	}, 'json');
}


function select_all(_this) {

	if ($(_this).attr('checked') == 'checked') {
		$(".pro-list input[type=checkbox]").each(function(index, element) {

			this.checked = true;
		});


	} else {
		$(".pro-list input[type=checkbox]").each(function(index, element) {

			$(this).removeAttr("checked");
		});

	}
}

function del_all(model, action_name) {

	var a = typeof(action_name) == 'undefined' ? 'del' : action_name;
	var m = typeof(model) == 'undefined' ? (getQueryString('m')) : model;
	var g = (getQueryString('g'));
	var check_arr = new Array;


	if (window.confirm('确定要删除全部吗？')) {
		$(".pro-list .pro-id input[type=checkbox]").each(function(index, element) {

			if ($(this).attr('checked') == 'checked') {

				check_arr.push($(this).val())
				$(this).parent().parent().remove();
			}
		});


		$.post('index.php?g=' + g + '&m=' + m + '&a=' + a, {
			id: check_arr
		}, function(result) {

			if (result.error == 0) {
				alert('删除成功');
				
			}else
			{
				//window.history.go(0);
			}
		}, 'json');

	}
}

function layout() {
	var height = $(window).height() - 40 - 53 - 65;
	if (height < 750) {
		height = 750;
	}
	$("#col").css('min-height', height + 'px');
}